package conqueror.com.systemupdate;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import conqueror.com.systemupdate.adapter.UpdateViewPagerAdapter;
import conqueror.com.systemupdate.base.BaseActivity;
import conqueror.com.systemupdate.fragment.LocalUpdateFragment;
import conqueror.com.systemupdate.fragment.OnlineCheckFragment;
import conqueror.com.systemupdate.fragment.OnlineUpdateFragment;
import conqueror.com.systemupdate.util.PreferenceUtils;
import conqueror.com.systemupdate.view.NoScrollViewPager;

public class MainActivity extends BaseActivity {

    private RadioGroup mRgContainer;
    private NoScrollViewPager mViewPager;
    private static List<Fragment> fragments;
    private ImageView mIvBack;
    private ImageView mIvMessageShow;
    private UpdateViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
        //设置默认选中项和加载
        mRgContainer.check(R.id.rb_localUpdate);
        mViewPager.setCurrentItem(1, false);
        adapter.notifyDataSetChanged();
    }


    private void initView() {
        mRgContainer = (RadioGroup) findViewById(R.id.rg_container);
        mViewPager = (NoScrollViewPager) findViewById(R.id.viewPager);

        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvMessageShow = (ImageView) findViewById(R.id.iv_messageShow);
    }

    private void initData() {
        fragments = new ArrayList<>();
        OnlineUpdateFragment onlineUpdateFragment = new OnlineUpdateFragment();
        LocalUpdateFragment localUpdateFragment = new LocalUpdateFragment();
        fragments.add(onlineUpdateFragment);
        fragments.add(localUpdateFragment);

        adapter = new UpdateViewPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(0);
        String display = Build.DISPLAY;

    }


    private void initListener() {
        mRgContainer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_onlineUpdate:
                        mViewPager.setCurrentItem(0, false);
                        break;
                    case R.id.rb_localUpdate:
                        mViewPager.setCurrentItem(1, false);
                        adapter.getItemPosition(1);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        });

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fragment = PreferenceUtils.getString(getApplicationContext(), "Fragment");
                if (fragment != null && fragment.equals("OnlineDownloadFragment")) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    OnlineCheckFragment onlineCheckFragment = new OnlineCheckFragment();
                    fragmentTransaction.replace(R.id.fl_onlineContainer, onlineCheckFragment);
                    fragmentTransaction.commit();
                } else {
                    finish();
                }

            }
        });

        mIvMessageShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                showPopwindow();
            }
        });

    }


    @Override
    protected void onStart() {
        super.onStart();
        adapter.notifyDataSetChanged();
    }


    /**
     * 显示popupWindow
     */
    private void showPopwindow() {
        // 利用layoutInflater获得View
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pop_window, null);

        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        PopupWindow window = new PopupWindow(view,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        window.setFocusable(true);


        // 设置popWindow的显示和消失动画
        window.setAnimationStyle(BIND_ABOVE_CLIENT);

        window.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);

        //popWindow消失监听方法
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                System.out.println("popWindow消失");
            }
        });

    }

}
